﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum GroupSubData
{
    GroupBaseData,
    ActionList
}
public class Action_BaseDataPanel : MonoBehaviour
{
    public PanelTween pageTween;
    public Button baseDataBtn, ActionBtn;
    public ActionListItem actionItemPfb;
    public Transform viewRoot, cacheRoot;
    /// <summary>
    /// InspectorRoot -- UnitActionInfo 面板
    /// </summary>
    public GameObject actionListRoot,InspectorRoot;

    private GroupSubData curPanel;
    private Data.ActionGroup curGroupData;
    //private Dictionary<Data.Action,ActionListItem> cacheItems =new Dictionary<Data.Action,ActionListItem>();
    private UIObjCacheProcess<ActionListItem> itemPool;
    private void OnEnable()
    {
        itemPool = new UIObjCacheProcess<ActionListItem>(cacheRoot, viewRoot, actionItemPfb);
        Rigisger();
    }
    private void OnDisable()
    {
        UnRigster();
    }

    /// <summary>
    /// 打开面板
    /// </summary>
    /// <param name="type"></param>
    /// <param name="groupData"></param>
    public void OpenPanel(GroupSubData type,Data.ActionGroup groupData)
    {
        this.curGroupData = groupData;
        //InitAction
        CreateActionList();
        //TODO Init BaseData
        CreateGroupBaseDataInspector();
    }
    private void Rigisger()
    {
        EventManager.AddEventListener(CommonEvent.OnOneActionDataRefreshActionInspector, OnOneActionDataRefreshActionInspector);
        EventManager.AddEventListener<Data.Action, ActionListItem>(CommonEvent.DelatetOneActionData, DelateOneAction);
    }
    private void UnRigster()
    {
        EventManager.AddEventListener(CommonEvent.OnOneActionDataRefreshActionInspector, OnOneActionDataRefreshActionInspector);
        EventManager.RemoveEventListener<Data.Action, ActionListItem>(CommonEvent.DelatetOneActionData, DelateOneAction);
    }

    private GameObject GroupBaseDataObj = null;
    /// <summary>
    /// 编辑基础数据
    /// </summary>
    public void OpenEditorBaseData()
    {
        ActionEditorInterface.SelectObj(GroupBaseDataObj);
    }

    /// <summary>
    /// 面板数据变更后 对action 面板进行数据刷新
    /// </summary>
    private void OnOneActionDataRefreshActionInspector()
    {
        CreateActionList();
    }
    /// <summary>
    /// 删除一个数据
    /// </summary>
    /// <param name="action"></param>
    private void DelateOneAction(Data.Action action, ActionListItem item)
    {
        if (itemPool.Recycle(item))
        {
            curGroupData.ActionList.Remove(action);
        }
    }

    /// <summary>
    /// 添加一个数据
    /// </summary>
    public void AddOneAction()
    {
        var newAction = new Data.Action();
        curGroupData.ActionList.Add(newAction);
        var item = itemPool.Spawn();

        item.SetData(newAction, null);
    }

    /// <summary>
    /// 刷新Action列表
    /// </summary>
    private void CreateActionList()
    {
        itemPool.RecycleAll();
        for (int i = 0; i < curGroupData.ActionList.Count; i++)
        {
            var item = itemPool.Spawn();
            var action = curGroupData.ActionList[i];
            var clip = CameraModelCtr.Ins.GetAnimClip(GetActionAnimName(action));
            item.SetData(action, clip);
        }
    }
    private void CreateGroupBaseDataInspector()
    {
        if (GroupBaseDataObj != null)
        {
            Destroy(GroupBaseDataObj);
        }
        else
        {
            GroupBaseDataObj = new GameObject("GroupBaseData");
            var comp =  GroupBaseDataObj.AddComponent<GroupBaseData>();
            comp.SetData(curGroupData);
            GroupBaseDataObj.transform.SetParent(InspectorRoot.transform);

        }
    }
    /// <summary>
    /// TODO 动画联合情况
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    private string GetActionAnimName(Data.Action data)
    {
        if (data.AnimSlotList.Count > 0)
        {
            return data.AnimSlotList[0].Animation;
        }
        else
        {
            return  string.Empty;
        }
    }
    /// <summary>
    /// 点击基础数据按钮
    /// </summary>
    public void BaseDateBtnClick()
    {
        baseDataBtn.enabled = false;
        ActionBtn.enabled = true;
        //TODO base data show
        
        pageTween.OpenSubActionList(false);
        pageTween.BaseViewActionSwitch(false);
    }
    /// <summary>
    /// 点击动作按钮
    /// </summary>
    public void ActionDataBtnClick()
    {
        baseDataBtn.enabled = true;
        ActionBtn.enabled = false;
       // actionListRoot.SetActive(true);
        pageTween.OpenSubActionList(true);
        pageTween.BaseViewActionSwitch(true);

        //todo 打开时候还是需要刷新下subaction的内容 ，切出去的时候action 会置于无选择状态的
    }

}
